home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / WD_SRC.ZIP / SYSTEM / DYNARRAY.CPP next >
C/C++ Source or Header  |  1994-12-19  |  14KB  |  700 lines

  1. #include "..\Source\LastWolf.hpp"
  2.  
  3.  
  4. // Functions for dynamic array classes 
  5. // Goes:  DWORD functions, WORD functions, BYTE functions, and Pointer functions
  6.  
  7.  
  8. // DWORD functions
  9. //-------------------------------------------------
  10.  
  11.  
  12. MDWordArray::MDWordArray()
  13. {
  14.     pDWordArray = NULL;
  15.     nElements = 0;  
  16. }
  17.  
  18.  
  19. MDWordArray::~MDWordArray()
  20. {
  21.     if( nElements > 0 )
  22.         free( pDWordArray );
  23. }
  24.  
  25.  
  26. MDWordArray &MDWordArray::operator=( MDWordArray ©From )
  27. {
  28.     SetSize( copyFrom.nElements );
  29.     
  30.     memcpy( pDWordArray, copyFrom.pDWordArray, sizeof(DWORD)*nElements );
  31.     
  32.     return *this;
  33. }
  34.  
  35.  
  36. DWORD MDWordArray::Get( UWORD index )
  37. {
  38. #ifdef CHECK_DYNARRAYS
  39.     CheckIndex(index);
  40. #endif
  41.     
  42.     return pDWordArray[index];
  43. }
  44.  
  45.  
  46. BOOL MDWordArray::Set( UWORD index, DWORD newVal )
  47. {
  48. #ifdef CHECK_DYNARRAYS
  49.     CheckIndex(index);
  50. #endif
  51.     
  52.     pDWordArray[index] = newVal;
  53.     
  54.     return TRUE;
  55. }
  56.  
  57.  
  58. UWORD MDWordArray::NumElements()
  59. {
  60.     return nElements;
  61. }
  62.  
  63. BOOL MDWordArray::Insert( UWORD index, DWORD toInsert )
  64. {
  65.     DWORD *newArray;
  66.     
  67. #ifdef CHECK_DYNARRAYS
  68.     ++nElements;
  69.     CheckIndex(index);
  70.     --nElements;
  71. #endif
  72.  
  73.     // Create a new array.
  74.     newArray = new DWORD[nElements+1];        
  75.  
  76.     // Copy the old array into the new one, start inserting at index.
  77.     memcpy( newArray, pDWordArray, index*sizeof(DWORD) );
  78.     memcpy( &newArray[index+1], &pDWordArray[index], (nElements-index)*sizeof(DWORD) );
  79.  
  80.     ++nElements;
  81.  
  82.     // Free the old array and set our pointer to the new one
  83.     free( pDWordArray );
  84.     pDWordArray = newArray;     
  85.     
  86.     // Put the new value in there
  87.     Set( index, toInsert );
  88.     
  89.     return TRUE;
  90. }              
  91.  
  92.  
  93. BOOL MDWordArray::Delete( UWORD index )
  94. {
  95.     DWORD *newArray;
  96.     UWORD copyIndex;
  97.     BOOL bDeleted;
  98.     
  99. #ifdef CHECK_DYNARRAYS
  100.     CheckIndex(index);
  101. #endif
  102.  
  103.     // Special case where we don't need to realloc a new array and copy...
  104.     if( --nElements == 0 )
  105.     {
  106.         free( pDWordArray );
  107.         pDWordArray = NULL;
  108.         
  109.         return TRUE; 
  110.     }
  111.     
  112.     // Create a new array
  113.     newArray = new DWORD[nElements];      
  114.  
  115.     // Copy the old array into the new one, skipping the one at index
  116.     bDeleted = FALSE;
  117.     for( copyIndex=0; copyIndex < NumElements(); copyIndex++ )
  118.     {
  119.         // If we're at index, skip the one we want to delete
  120.         if( copyIndex == index )
  121.             bDeleted = TRUE;
  122.         
  123.         if( bDeleted == TRUE )
  124.             newArray[copyIndex] = pDWordArray[copyIndex+1];
  125.         else
  126.             newArray[copyIndex] = pDWordArray[copyIndex];
  127.     }
  128.  
  129.     // Free the old array and set our pointer to the new one
  130.     free( pDWordArray );
  131.     pDWordArray = newArray;     
  132.     
  133.     return TRUE;
  134. }
  135.  
  136.  
  137. BOOL MDWordArray::Append( DWORD toAdd )
  138. {
  139.     // Just insert an element at the end
  140.     return Insert( nElements, toAdd );
  141. }
  142.  
  143.  
  144. BOOL MDWordArray::SetSize( UWORD newSize )
  145. {
  146.     free( pDWordArray );
  147.     pDWordArray = NULL;
  148.     
  149.     nElements = newSize;
  150.  
  151.     if( newSize > 0 )
  152.         pDWordArray = new DWORD[newSize];
  153.  
  154.     return TRUE;
  155. }
  156.  
  157. BOOL MDWordArray::CheckIndex( UWORD index )
  158. {
  159.     char error[256];
  160.     
  161.     if( index < nElements )
  162.         return TRUE;
  163.     
  164.     sprintf( error, "Invalid index, %d, attempted in MDWordArray, exiting.", index );
  165.     puts( error );
  166.     
  167.     return FALSE;
  168. }
  169.  
  170.  
  171.  
  172. // WORD functions
  173. //-------------------------------------------------
  174.  
  175.  
  176. MWordArray::MWordArray()
  177. {
  178.     pWordArray = NULL;
  179.     nElements = 0;  
  180. }
  181.  
  182.  
  183. MWordArray::~MWordArray()
  184. {
  185.     if( nElements > 0 )
  186.         free( pWordArray );
  187. }
  188.  
  189.  
  190. MWordArray &MWordArray::operator=( MWordArray ©From )
  191. {
  192.     SetSize( copyFrom.nElements );
  193.     
  194.     memcpy( pWordArray, copyFrom.pWordArray, sizeof(WORD)*nElements );
  195.  
  196.     return *this;
  197. }
  198.  
  199.  
  200. WORD MWordArray::Get( UWORD index )
  201. {
  202. #ifdef CHECK_DYNARRAYS
  203.     CheckIndex(index);
  204. #endif
  205.  
  206.     return pWordArray[index];
  207. }
  208.  
  209.  
  210. BOOL MWordArray::Set( UWORD index, WORD newVal )
  211. {
  212. #ifdef CHECK_DYNARRAYS
  213.     CheckIndex(index);
  214. #endif
  215.  
  216.     pWordArray[index] = newVal;
  217.     
  218.     return TRUE;
  219. }
  220.  
  221.  
  222. UWORD MWordArray::NumElements()
  223. {
  224.     return nElements;
  225. }                               
  226.  
  227. BOOL MWordArray::Insert( UWORD index, WORD toInsert )
  228. {
  229.     WORD *newArray;
  230.     
  231. #ifdef CHECK_DYNARRAYS
  232.     ++nElements;
  233.     CheckIndex(index);
  234.     --nElements;
  235. #endif
  236.  
  237.     // Create a new array
  238.     newArray = new WORD[nElements+1];     
  239.  
  240.     // Copy the old array into the new one, start inserting at index.
  241.     memcpy( newArray, pWordArray, index*sizeof(DWORD) );
  242.     memcpy( &newArray[index+1], &pWordArray[index], (nElements-index)*sizeof(DWORD) );
  243.  
  244.     ++nElements;
  245.  
  246.     // Free the old array and set our pointer to the new one
  247.     free( pWordArray );
  248.     pWordArray = newArray;      
  249.     
  250.     // Put the new value in there
  251.     Set( index, toInsert );
  252.     
  253.     return TRUE;
  254. }              
  255.  
  256.  
  257. BOOL MWordArray::Delete( UWORD index )
  258. {
  259.     WORD *newArray;
  260.     UWORD copyIndex;
  261.     BOOL bDeleted;
  262.  
  263. #ifdef CHECK_DYNARRAYS
  264.     CheckIndex(index);
  265. #endif
  266.     
  267.     // Special case where we don't need to realloc a new array and copy...
  268.     if( --nElements == 0 )
  269.     {
  270.         free( pWordArray );
  271.         pWordArray = NULL;
  272.         
  273.         return TRUE; 
  274.     }
  275.     
  276.     // Create a new array
  277.     newArray = new WORD[nElements];       
  278.  
  279.     // Copy the old array into the new one, skipping the one at index
  280.     bDeleted = FALSE;
  281.     for( copyIndex=0; copyIndex < NumElements(); copyIndex++ )
  282.     {
  283.         // If we're at index, skip the one we want to delete
  284.         if( copyIndex == index )
  285.             bDeleted = TRUE;
  286.         
  287.         if( bDeleted == TRUE )
  288.             newArray[copyIndex] = pWordArray[copyIndex+1];
  289.         else
  290.             newArray[copyIndex] = pWordArray[copyIndex];
  291.     }
  292.  
  293.     // Free the old array and set our pointer to the new one
  294.     free( pWordArray );
  295.     pWordArray = newArray;      
  296.     
  297.     return TRUE;
  298. }
  299.  
  300.  
  301. BOOL MWordArray::Append( WORD toAdd )
  302. {
  303.     // Just insert an element at the end
  304.     return Insert( nElements, toAdd );
  305. }
  306.  
  307.  
  308. BOOL MWordArray::SetSize( UWORD newSize )
  309. {
  310.     free( pWordArray );
  311.     pWordArray = NULL;
  312.     
  313.     nElements = newSize;
  314.  
  315.     if( newSize > 0 )
  316.         pWordArray = new WORD[newSize];
  317.  
  318.     return TRUE;
  319. }
  320.  
  321.  
  322. BOOL MWordArray::CheckIndex( UWORD index )
  323. {
  324.     char error[256];
  325.     
  326.     if( index < nElements )
  327.         return TRUE;
  328.     
  329.     sprintf( error, "Invalid index, %d, attempted in MWordArray, exiting.", index );
  330.     puts( error );
  331.     
  332.     return FALSE;
  333. }
  334.  
  335.  
  336.  
  337.  
  338.  
  339. // BYTE functions
  340. //-------------------------------------------------
  341.  
  342.  
  343. MByteArray::MByteArray()
  344. {
  345.     pByteArray = NULL;
  346.     nElements = 0;  
  347. }
  348.  
  349.  
  350. MByteArray::~MByteArray()
  351. {
  352.     if( nElements > 0 )
  353.         free( pByteArray );
  354. }
  355.  
  356.  
  357. MByteArray &MByteArray::operator=( MByteArray ©From )
  358. {
  359.     SetSize( copyFrom.nElements );
  360.     
  361.     memcpy( pByteArray, copyFrom.pByteArray, sizeof(BYTE)*nElements );
  362.  
  363.     return *this;
  364. }
  365.  
  366.  
  367. BYTE MByteArray::Get( UWORD index )
  368. {
  369. #ifdef CHECK_DYNARRAYS
  370.     CheckIndex(index);
  371. #endif
  372.  
  373.     return pByteArray[index];
  374. }
  375.  
  376.  
  377. BOOL MByteArray::Set( UWORD index, BYTE newVal )
  378. {
  379. #ifdef CHECK_DYNARRAYS
  380.     CheckIndex(index);
  381. #endif
  382.  
  383.     pByteArray[index] = newVal;
  384.     
  385.     return TRUE;
  386. }
  387.  
  388.  
  389. UWORD MByteArray::NumElements()
  390. {
  391.     return nElements;
  392. }
  393.  
  394. BOOL MByteArray::Insert( UWORD index, BYTE toInsert )
  395. {
  396.     BYTE *newArray;
  397.     
  398. #ifdef CHECK_DYNARRAYS
  399.     ++nElements;
  400.     CheckIndex(index);
  401.     --nElements;
  402. #endif
  403.  
  404.     // Create a new array
  405.     newArray = new BYTE[nElements+1];     
  406.  
  407.     // Copy the old array into the new one, start inserting at index.
  408.     memcpy( newArray, pByteArray, index*sizeof(DWORD) );
  409.     memcpy( &newArray[index+1], &pByteArray[index], (nElements-index)*sizeof(DWORD) );
  410.  
  411.     ++nElements;
  412.  
  413.     // Free the old array and set our pointer to the new one
  414.     free( pByteArray );
  415.     pByteArray = newArray;      
  416.     
  417.     // Put the new value in there
  418.     Set( index, toInsert );
  419.     
  420.     return TRUE;
  421. }              
  422.  
  423.  
  424. BOOL MByteArray::Delete( UWORD index )
  425. {
  426.     BYTE *newArray;
  427.     UWORD copyIndex;
  428.     BOOL bDeleted;
  429.     
  430. #ifdef CHECK_DYNARRAYS
  431.     CheckIndex(index);
  432. #endif
  433.  
  434.     // Special case where we don't need to realloc a new array and copy...
  435.     if( --nElements == 0 )
  436.     {
  437.         free( pByteArray );
  438.         pByteArray = NULL;
  439.         
  440.         return TRUE; 
  441.     }
  442.     
  443.     // Create a new array
  444.     newArray = new BYTE[nElements];       
  445.  
  446.     // Copy the old array into the new one, skipping the one at index
  447.     bDeleted = FALSE;
  448.     for( copyIndex=0; copyIndex < NumElements(); copyIndex++ )
  449.     {
  450.         // If we're at index, skip the one we want to delete
  451.         if( copyIndex == index )
  452.             bDeleted = TRUE;
  453.         
  454.         if( bDeleted == TRUE )
  455.             newArray[copyIndex] = pByteArray[copyIndex+1];
  456.         else
  457.             newArray[copyIndex] = pByteArray[copyIndex];
  458.     }
  459.  
  460.     // Free the old array and set our pointer to the new one
  461.     free( pByteArray );
  462.     pByteArray = newArray;      
  463.     
  464.     return TRUE;
  465. }
  466.  
  467.  
  468. BOOL MByteArray::Append( BYTE toAdd )
  469. {
  470.     // Just insert an element at the end
  471.     return Insert( nElements, toAdd );
  472. }
  473.  
  474.  
  475. BOOL MByteArray::SetSize( UWORD newSize )
  476. {
  477.     free( pByteArray );
  478.     pByteArray = NULL;
  479.     
  480.     nElements = newSize;
  481.  
  482.     if( newSize > 0 )
  483.         pByteArray = new BYTE[newSize];
  484.  
  485.     return TRUE;
  486. }
  487.  
  488.  
  489. BOOL MByteArray::CheckIndex( UWORD index )
  490. {
  491.     char error[256];
  492.     
  493.     if( index < nElements )
  494.         return TRUE;
  495.     
  496.     sprintf( error, "Invalid index, %d, attempted in MByteArray, exiting.", index );
  497.     puts( error );
  498.     
  499.     return FALSE;
  500. }
  501.  
  502.  
  503.  
  504. // Pointer functions
  505. //-------------------------------------------------
  506.  
  507.  
  508. MPointerArray::MPointerArray()
  509. {
  510.     pPointerArray = NULL;
  511.     nElements = 0;  
  512. }
  513.  
  514.  
  515. MPointerArray::~MPointerArray()
  516. {
  517.     if( nElements > 0 )
  518.         free( pPointerArray );
  519. }
  520.  
  521.  
  522. MPointerArray &MPointerArray::operator=( MPointerArray ©From )
  523. {
  524.     SetSize( copyFrom.nElements );
  525.     
  526.     memcpy( pPointerArray, copyFrom.pPointerArray, sizeof(void *)*nElements );
  527.  
  528.     return *this;
  529. }
  530.  
  531.  
  532. void *MPointerArray::Get( UWORD index )
  533. {
  534. #ifdef CHECK_DYNARRAYS
  535.     CheckIndex(index);
  536. #endif
  537.  
  538.     return pPointerArray[index];
  539. }
  540.  
  541.  
  542. BOOL MPointerArray::Set( UWORD index, void *newVal )
  543. {
  544. #ifdef CHECK_DYNARRAYS
  545.     CheckIndex(index);
  546. #endif
  547.  
  548.     pPointerArray[index] = newVal;
  549.     
  550.     return TRUE;
  551. }
  552.  
  553.  
  554. UWORD MPointerArray::NumElements()
  555. {
  556.     return nElements;
  557. }
  558.  
  559. BOOL MPointerArray::Insert( UWORD index, void *toInsert )
  560. {
  561.     void  **newArray;
  562.     
  563. #ifdef CHECK_DYNARRAYS
  564.     ++nElements;
  565.     CheckIndex(index);
  566.     --nElements;
  567. #endif
  568.  
  569.     // Create a new array
  570.     newArray = new void *[nElements+1];     
  571.  
  572.     // Copy the old array into the new one, start inserting at index.
  573.     memcpy( newArray, pPointerArray, index*sizeof(DWORD) );
  574.     memcpy( &newArray[index+1], &pPointerArray[index], (nElements-index)*sizeof(DWORD) );
  575.  
  576.     ++nElements;
  577.  
  578.     // Free the old array and set our pointer to the new one
  579.     free( pPointerArray );
  580.     pPointerArray = newArray;       
  581.  
  582.     // Put the new value in there
  583.     Set( index, toInsert );
  584.     
  585.     return TRUE;
  586. }              
  587.  
  588.  
  589. BOOL MPointerArray::Delete( UWORD index )
  590. {
  591.     void  **newArray;
  592.     UWORD copyIndex;
  593.     BOOL bDeleted;
  594.     
  595. #ifdef CHECK_DYNARRAYS
  596.     CheckIndex(index);
  597. #endif
  598.  
  599.     // Special case where we don't need to realloc a new array and copy...
  600.     if( --nElements == 0 )
  601.     {
  602.         free( pPointerArray );
  603.         pPointerArray = NULL;
  604.         
  605.         return TRUE; 
  606.     }
  607.     
  608.     // Create a new array
  609.     newArray = new void *[nElements];       
  610.  
  611.     // Copy the old array into the new one, skipping the one at index
  612.     bDeleted = FALSE;
  613.     for( copyIndex=0; copyIndex < NumElements(); copyIndex++ )
  614.     {
  615.         // If we're at index, skip the one we want to delete
  616.         if( copyIndex == index )
  617.             bDeleted = TRUE;
  618.         
  619.         if( bDeleted == TRUE )
  620.             newArray[copyIndex] = pPointerArray[copyIndex+1];
  621.         else
  622.             newArray[copyIndex] = pPointerArray[copyIndex];
  623.     }
  624.  
  625.     // Free the old array and set our pointer to the new one
  626.     free( pPointerArray );
  627.     pPointerArray = newArray;       
  628.     
  629.     return TRUE;
  630. }
  631.  
  632.  
  633. BOOL MPointerArray::Append( void *toAdd )
  634. {
  635.     // Just insert an element at the end
  636.     return Insert( nElements, toAdd );
  637. }
  638.  
  639.  
  640. BOOL MPointerArray::SetSize( UWORD newSize )
  641. {
  642.     free( pPointerArray );
  643.     pPointerArray = NULL;
  644.     
  645.     nElements = newSize;
  646.  
  647.     if( newSize > 0 )
  648.         pPointerArray = new void *[newSize];
  649.  
  650.     return TRUE;
  651. }
  652.  
  653.  
  654. Index MPointerArray::GetIndexFromPointer( void *pMatch )
  655. {
  656.     WORD retVal;
  657.  
  658.     retVal = ScanForPointer( pMatch, pPointerArray, nElements );
  659.     
  660.     if( retVal != -1 )
  661.         return (Index)(nElements - retVal - 1);
  662.     else
  663.         return BAD_INDEX;
  664.     
  665. /*
  666.     for( i=0; i < nElements; i++ )
  667.         if( pPointerArray[i] == pMatch )
  668.             return i;
  669. */            
  670. }
  671.  
  672.  
  673. BOOL MPointerArray::CheckIndex( UWORD index )
  674. {
  675.     char error[256];
  676.     
  677.     if( index < nElements )
  678.         return TRUE;
  679.     
  680.     sprintf( error, "Invalid index, %d, attempted in MPointerArray, exiting.", index );
  681.     puts( error );
  682.     
  683.     return FALSE;
  684. }
  685.  
  686.  
  687.  
  688. #pragma aux ScanForPointer = \
  689. "repnz scasd"    \
  690. "sub edi, 4"    \
  691. "cmp eax,[edi]"    \
  692. "je FOUND"    \
  693. "mov cx,-1"    \
  694. "FOUND:"    \
  695. value [cx]    \
  696. parm [eax] [edi] [ecx];
  697.  
  698.  
  699.  
  700.